<include file="public@header"/>
</head>
<body>
<div class="wrap js-check-wrap">
    <ul class="nav nav-tabs">
        <li class="active"><a href="{:url('AdminInvoice/waitPrint')}">待开普票</a></li>
        <li><a href="{:url('AdminInvoice/waitPrintValueAdded')}">待开专票</a></li>
    </ul>
    <div  class="margin-top-20">
        <div class="table-actions">
            <button class="btn btn-primary btn-sm js-download-invoices-xml-btn" type="button" data-action=""
                    data-subcheck="true" data-msg="你确定批量开票吗？">批量开票
            </button>
            <form class="form-inline js-ajax-form" method="post" action="{:url('AdminInvoice/uploadInvoicesXml')}"
                  id="upload-invoice-xml-file-form" style="display: inline-block;">
                <input type="hidden" id="upload-invoice-xml-file-input" name="file" class="form-control" style="width: 300px;"/>
                <a href="javascript:openUploadDialog('上传已开发票',_uploadInvoiceXmlFileCallback,{},false,'file')"
                   class="btn btn-primary btn-sm">上传已开发票</a>
                <button type="submit" class="js-ajax-submit" id="upload-invoice-xml-file-submit" style="display: none;"></button>
            </form>
        </div>
        <php>$status=array("1"=>lang('DISPLAY'),"0"=>lang('HIDDEN'));</php>
        <table class="table table-hover table-bordered table-list">
            <thead>
            <tr>
                <th width="16">
                    <label>
                        <input type="checkbox" class="js-check-all" data-direction="x" data-checklist="js-check-x">
                    </label>
                </th>
                <th width="50">ID</th>
                <th width="20%">发票号</th>
                <th>开票类型</th>
                <th>单位名称</th>
                <th>订单信息</th>
                <th>申请时间</th>
                <th width="120">{:lang('ACTIONS')}</th>
            </tr>
            </thead>
            <tbody>
            <foreach name="invoices" item="vo">
                <tr>
                    <td>
                        <input type="checkbox" class="js-check" data-yid="js-check-y" data-xid="js-check-x" name="ids[]"
                               value="{$vo.id}">
                    </td>
                    <td>{$vo.id}</td>
                    <td>
                        <input class="form-control input-invoice_no" type="text"
                               placeholder="{$vo.invoice_no|default='未开发票'}"
                               style="width: 200px;display: inline-block;" data-id="{$vo.id}"/>
                    </td>
                    <td>
                        <eq name="vo.type" value="2">
                            普通发票
                        </eq>
                        <eq name="vo.type" value="3">
                            专用发票
                        </eq>
                    </td>
                    <td>{$vo.title}</td>

                    <td>
                        <php>$invoice_amount=0;</php>
                        <foreach name="vo.orders" item="order">
                            <php>$invoice_amount+=$order['order_amount'];</php>
                            <div>{$order.order_sn} <b>￥{$order.order_amount}</b></div>
                        </foreach>
                        <div>总额:￥{$invoice_amount}</div>
                    </td>
                    <td>{:date('Y-m-d',$vo.create_time)}<br>{:date('H:i:s',$vo.create_time)}</td>
                    <td>
                        <a href="javascript:parent.openIframeLayer('{:url('order/AdminInvoice/detail',['id'=>$vo['id']])}','发票详情',{});">详情</a>
                        <a href="javascript:parent.openIframeLayer('{:url('order/AdminInvoice/notPrintedInvoiceOrders',['id'=>$vo['id']])}','关联订单',{});">关联订单</a>
                    </td>
                </tr>
            </foreach>
            </tbody>
            <tfoot>
            <tr>
                <th width="16">
                    <label>
                        <input type="checkbox" class="js-check-all" data-direction="x" data-checklist="js-check-x">
                    </label>
                </th>
                <th width="50">ID</th>
                <th>发票号</th>
                <th>开票类型</th>
                <th>单位名称</th>
                <th>订单信息</th>
                <th>申请时间</th>
                <th width="120">{:lang('ACTIONS')}</th>
            </tr>
            </tfoot>
        </table>
    </div>
</div>
<script src="__STATIC__/js/admin.js"></script>
<script>
    function _uploadInvoiceXmlFileCallback(dialog, files, params) {
        $('#upload-invoice-xml-file-input').val(files[0]['filepath']);
        //$('#upload-invoice-xml-file-form').submit();
        $('#upload-invoice-xml-file-submit').click();
    }
    
    $('.js-download-invoices-xml-btn').click(function () {
        var ids = [];
        $("input[name='ids[]']").each(function () {
            if ($(this).is(':checked')) {
                ids.push($(this).val());
            }
        });

        if (ids.length == 0) {
            $('<span class="tips_error">请至少选择一项</span>').appendTo($(this).parent()).fadeIn('fast');
            return
        }

        ids = ids.join(',');
        parent.openIframeLayer("{:url('AdminInvoice/downloadInvoicesXml')}?ids=" + ids, '下载发票 xml', {
            area: ['95%', '90%'],
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                console.log(layero);
                var iframeWin = parent.window[layero.find('iframe')[0]['name']];
                //iframeWin.confirm();
                parent.layer.close(index); //如果设定了yes回调，需进行手工关闭
            },
            end: function () {
                location.reload(true);
            }
        });
    });

    $('.input-invoice_no').change(function () {
        var $this     = $(this);
        var id        = $this.data('id');
        var invoiceNo = $this.val();
        $.ajax({
            url: "{:url('AdminInvoice/setInvoiceNo')}",
            type: "POST",
            dataType: 'json',
            data: {id: id, invoice_no: invoiceNo},
            success: function (data) {


            },
            error: function () {

            }
        })
    });
</script>
</body>
</html>